import { app, shell } from 'electron'
import type { MenuItemConstructorOptions } from 'electron'

/**
 * 主菜单
 */
export const mainMenu = (browserWindow: Electron.BrowserWindow): MenuItemConstructorOptions[] => {
    const template: MenuItemConstructorOptions[] = [
        // { role: 'appMenu' }
        {
            label: app.name,
            submenu: [
                {
                    label: '测试1',
                    click: () => {
                        browserWindow.webContents.send('menu-command', '点击了主菜单“测试1”')
                    }
                },
                { type: 'separator' },
                { role: 'services' },
                { type: 'separator' },
                { role: 'hide' },
                { role: 'hideOthers' },
                { role: 'unhide' },
                { type: 'separator' },
                { role: 'quit' }
            ]
        },
        // { role: 'editMenu' }
        {
            label: '编辑',
            submenu: [
                { role: 'undo' },
                { role: 'redo' },
                { type: 'separator' },
                { role: 'cut' },
                { role: 'copy' },
                { role: 'paste' },
                { role: 'pasteAndMatchStyle' },
                { role: 'delete' },
                { role: 'selectAll' },
                { type: 'separator' },
                {
                    label: 'Speech',
                    submenu: [{ role: 'startSpeaking' }, { role: 'stopSpeaking' }]
                }
            ]
        },
        // { role: 'viewMenu' }
        {
            label: '查看',
            submenu: [
                { role: 'reload' },
                { role: 'forceReload' },
                { role: 'toggleDevTools' },
                { type: 'separator' },
                { role: 'resetZoom' },
                { role: 'zoomIn' },
                { role: 'zoomOut' },
                { type: 'separator' },
                { role: 'togglefullscreen' }
            ]
        },
        // { role: 'windowMenu' }
        {
            label: '窗口',
            submenu: [
                { role: 'minimize' },
                { role: 'zoom' },
                { type: 'separator' },
                { role: 'front' },
                { type: 'separator' },
                { role: 'window' }
            ]
        },
        {
            label: '帮助',
            role: 'help',
            submenu: [
                { label: '关于', role: 'about' },
                {
                    label: 'Learn More',
                    click: async () => {
                        await shell.openExternal('https://electronjs.org')
                    }
                }
            ]
        }
    ]
    return template
}

/**
 * 右键菜单
 */
export const contextMenu = (event: Electron.IpcMainEvent): MenuItemConstructorOptions[] => {
    const template: MenuItemConstructorOptions[] = [
        {
            label: '测试1',
            click: () => {
                event.sender.send('menu-command', '点击了右键菜单“测试1”')
            }
        },
        { type: 'separator' },
        { label: 'Menu Item 2', type: 'checkbox', checked: true }
    ]
    return template
}
